package client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

class MyWindowAdapter extends WindowAdapter {
	public void windowClosing(WindowEvent e) {
		System.exit(0);
	}
}

/**
 * @author papq
 *
 */
public class StartViewJFrame extends JFrame implements Observable {
	private Model model;

	private ArrayList<Observer> observers;

	private boolean changed;

	private static JTextField hostTextField = new JTextField();
	private static final JTextField portTextField = new JTextField();
	private static JTextField answearTextField = new JTextField();
	private static JButton buttonConnect = new JButton();
	private static JButton buttonStartGame = new JButton();

	private static String host = "";
	private static String portString = "";
	private static int port = 0;

	public StartViewJFrame(Model model) {
		super("Badugi Poker");
		this.model = model;
		observers = new ArrayList<Observer>();
		changed = false;

		setResizable(false);
		setAlwaysOnTop(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 400, 250);
		getContentPane().setLayout(null);

		JLabel hostLabel = new JLabel("Host nr:");
		hostLabel.setBounds(10, 10, 180, 25);
		getContentPane().add(hostLabel);

		JLabel portLabel = new JLabel("Port nr:");
		portLabel.setBounds(210, 10, 180, 25);
		getContentPane().add(portLabel);

		hostTextField.setBounds(10, 45, 180, 25);
		getContentPane().add(hostTextField);
		hostTextField.setColumns(10);
		hostTextField.setText("Enter host...");

		portTextField.setBounds(210, 45, 180, 25);
		getContentPane().add(portTextField);
		portTextField.setColumns(10);
		portTextField.setText("Enter port...");

		buttonConnect.setText("Connect");
		buttonConnect.setBounds(140, 85, 120, 25);
		getContentPane().add(buttonConnect);
		buttonConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (buttonConnect.getText().equals("Connect")) {
					StartViewJFrame.this.model.setHost(hostTextField.getText());
					portString = portTextField.getText();
					if (StartViewJFrame.this.model.getHost().equals(
							"Enter host...")
							|| StartViewJFrame.this.model.getHost().equals("")) {
						StartViewJFrame.this.model.setHost("127.0.0.1");
					}
					if (portString.equals("Enter port...")
							|| portString.equals("")) {
						StartViewJFrame.this.model.setPort(4444);
					} else {
						try {
							StartViewJFrame.this.model.setPort(Integer
									.parseInt(portString));
						} catch (NumberFormatException ex) {
						}
					}
					StartViewJFrame.this.model.setPressedButton("Connect");
					answearTextField.setText("Connecting...");
					setChanged();
					notifyObservers();
					answearTextField.setText(StartViewJFrame.this.model
							.getText());
				} else if (buttonConnect.getText().equals("Disconnect")) {
					StartViewJFrame.this.model.setPressedButton("Disconnect");
					setChanged();
					notifyObservers();
					answearTextField.setText(StartViewJFrame.this.model
							.getText());
				}
			}
		});

		answearTextField.setBounds(10, 125, 380, 25);
		answearTextField.setEditable(false);
		getContentPane().add(answearTextField);

		buttonStartGame.setText("Start Game");
		buttonStartGame.setBounds(140, 165, 120, 25);
		buttonStartGame.setEnabled(false);
		getContentPane().add(buttonStartGame);
		buttonStartGame.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				StartViewJFrame.this.model.setPressedButton("Start Game");
				buttonStartGame.setEnabled(false);
				String messageToServer = "<"
						+ StartViewJFrame.this.model.getPlayerID() + "><02><Y>";
				StartViewJFrame.this.model.setMessageToServer(messageToServer);
				setChanged();
				notifyObservers();
			}
		});
	}

	public static String getHost() {
		return host;
	}

	public static void setHost(String host) {
		StartViewJFrame.host = host;
	}

	public static int getPort() {
		return port;
	}

	public static void setPort(int port) {
		StartViewJFrame.port = port;
	}

	@Override
	public void addObserver(Observer observer) {
		observers.add(observer);
	}

	@Override
	public void removeObserver(Observer observer) {
		observers.remove(observer);
	}

	@Override
	public void notifyObservers() {
		if (changed) {
			for (Observer o : observers) {
				o.updateFromStartView();
			}
			changed = false;
		}
	}

	@Override
	public void setChanged() {
		changed = true;
	}

	public void informUserOfFailedConnection() {
		JOptionPane.showMessageDialog(null, "Connection failure");
		answearTextField.setText("Connection failure");
	}

	public void changeConnectButtonToDisconnect() {
		buttonConnect.setText("Disconnect");
	}

	public void changeDisconnectButtonToConnect() {
		buttonConnect.setText("Connect");
	}

	public void displayMessage() {
		answearTextField.setText(StartViewJFrame.this.model.getText());
	}

	public void askForStartGame() {
		buttonConnect.setEnabled(false);
		buttonStartGame.setEnabled(true);
		answearTextField
				.setText("We've all players. Press \"Start Game\" to play.");
	}

	public void displayMessage(String messageToDisplay) {
		answearTextField.setText(messageToDisplay);	
	}

}
